package com.work.controller;

import com.work.bean.Message;
import com.work.bean.Notice;
import com.work.bean.Student;
import com.work.bean.Teacher;
import com.work.dao.MessageDao;
import com.work.dao.impl.MessageDaoImpl;
import com.work.dto.Mstudent;
import com.work.service.MessageService;
import com.work.service.impl.MessageServiceImpl;
import com.work.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

public class MessageServlet extends BaseServlet{
    MessageService messageService= new MessageServiceImpl();
    MessageDao messageDao = new MessageDaoImpl();
    protected void  queryMessage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<Message> messages = messageService.queryMessage();
        req.setAttribute("ms",messages);
        req.getRequestDispatcher("/pages/admin/message/admin_message.jsp").forward(req,resp);
    }
    protected void  queryMessage1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        if (((Teacher) req.getSession().getAttribute("teacher"))== null){
            req.getRequestDispatcher("/pages/teacher/teacher_update.jsp").forward(req,resp);
        }else{
        int t_id = WebUtils.parseInt(((Teacher)req.getSession().getAttribute("teacher")).getId()+"",0);
        List<Mstudent> mstudents = messageService.queryMessage1(t_id);
        req.setAttribute("mss",mstudents);
        req.getRequestDispatcher("/pages/teacher/teacher.jsp").forward(req,resp);}
    }
    protected void saveMessage(HttpServletRequest req, HttpServletResponse resp)throws
            ServletException, IOException {
        int s_id = WebUtils.parseInt(((Student)req.getSession().getAttribute("student")).getId()+"",0);
        Message message = WebUtils.copyParamToBean(req.getParameterMap(),new Message());
        message.setS_id(s_id);
        message.setState(0);
        message.setTime(new Date());
        messageService.saveMessage(message);
        resp.sendRedirect(req.getContextPath()+"/NoticeServlet?action=page&pageNo=1");
    }
    protected void deleteMessage(HttpServletRequest req, HttpServletResponse resp)throws
            ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        messageService.deleteMessage(id);
        resp.sendRedirect(req.getContextPath()+"/MessageServlet?action=queryMessage");
    }
    protected void queryMessageBysId(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        int s_id = WebUtils.parseInt(req.getParameter("id"),0);
        Message message = messageService.queryMessageBysId(s_id);
        req.setAttribute("message",message);
        req.getRequestDispatcher("/pages/message/message_look").forward(req,resp);
    }
    protected void queryMessageById(HttpServletRequest req, HttpServletResponse resp) throws
            ServletException, IOException {
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        Message message = messageDao.queryMessageById(id);
        messageDao.updateMessage(id,1);
        req.setAttribute("message",message);
        req.getRequestDispatcher("/pages/message/message_look.jsp").forward(req,resp);
    }
    }
